package com.lu.jdog.annotation.closedevice.processor;


import com.google.auto.service.AutoService;
import com.lu.jdog.annotation.closedevice.annotation.Print;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.LinkedHashSet;
import java.util.Set;

@AutoService(Processor.class)
public class PrintProcessor extends AbstractProcessor {

    private Messager mMessager;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        mMessager = processingEnvironment.getMessager();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        // IDEA断点调试编译时注解，可以
        // 1、点击EditConfiguration -> + -> Remote JVM Debug
        // 2、复制参数-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
        // 3、粘贴参数到其他gradle任务中，重点：suspend=y 更改
        print(">>>>>>>>>>>pppppp[");
        for (TypeElement te : annotations) {
            for (Element e : roundEnv.getElementsAnnotatedWith(te)) {//find special annotationed element
                print("PrintProcessor:" + e.toString());//print element
            }

        }
        return true;
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {

        return SourceVersion.latestSupported();
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        LinkedHashSet<String> annotations = new LinkedHashSet<>();
        annotations.add(Print.class.getCanonicalName());
        return annotations;
    }

    private void print(String msg) {
        mMessager.printMessage(Diagnostic.Kind.NOTE, msg);
    }
}